local BufferExcel = require("excel.Buffer").Buff
local BufferCmdExcel = require("excel.Buffer").BuffCmd
local Timer = require("core.Timer")

local setTipArgs = {} --设置BUF显示效果参数

function setTipArgs.hpShanghai(obj,buf,args)
	local config = BufferExcel[buf.id]
	args[0] = 2
	args[1] = config.speed
	args[2] = buf.param or 0
end

function setTipArgs.hpRecover(obj,buf,args)
  local config = BufferExcel[buf.id]
  args[0] = 2
  args[1] = config.speed
  args[2] = buf.param or 0
end

function setTipArgs.mp(obj,buf,args)
  local config = BufferExcel[buf.id]
  args[0] = 2
  args[1] = config.speed
  args[2] = buf.param or 0
end

function setTipArgs.gedang(obj,buf,args)
  args[0] = 1
  args[1] = buf.param or 0
end

function setTipArgs.shanghaiOffset(obj,buf,args)
  args[0] = 1
  args[1] = buf.param
end
function setTipArgs.immunityBuf(obj,buf,args)
  args[0] = 0
  return obj, buf
end

function setTipArgs.shanghaiToAtk(obj,buf,args)
  local config = BufferExcel[buf.id]
  args[0] = 2
  args[1] = config.speed
  args[2] = buf.param or 0
end

local function getAppendArg(buf,config,cmdConfig,arg)
    arg = arg or config.arg
    local appendArg = cmdConfig.appendArg
    if config.valCanAppend == 1 then
        return  appendArg[1]==1 and arg[1]*buf.count or arg[1] ,
        appendArg[2]==1 and arg[2]*buf.count or arg[2] ,
        appendArg[3]==1 and arg[3]*buf.count or arg[3] ,
        appendArg[4]==1 and arg[4]*buf.count or arg[4] ,
        appendArg[5]==1 and arg[5]*buf.count or arg[5],
        appendArg[6]==1 and arg[6]*buf.count or arg[6]
    else
        return arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]
    end
end

local function SetTipArgs(obj,buf,args)
  local config = BufferExcel[buf.id]
  local cmdConfig = BufferCmdExcel[config.cmd]
  if not cmdConfig then
    args[0] = 0
    if string.len(config.cmd)>0 then
      print("[ERROR]:setTipArgs",buf.id,config.cmd)
    end
    return
  end
  
  args[1],args[2],args[3],args[4],args[5] = getAppendArg(buf,config,cmdConfig)

  local len = 0               
  for i=1,5 do
    if args[i] then
      len = len + 1
    else
      break;
    end
  end       
  args[0] = len             
end

function setBuffInfo(obj, buff, buf, config)
    buff.id = buf.id
    buff.count = buf.count
    buff.iconPosType = config.iconPosType
    buff.icon = config.iconPath
    buff.name = config.name
    buff.actionTip = config.actionTip
    buff.time = buf.time
    buff.isGood = config.isGood
    buff.lastTime = buf.time-(Timer.now - buf.startTime)     
    local setTipArgsCallBack = setTipArgs[config.cmd] or SetTipArgs
    setTipArgsCallBack(obj,buf,buff.args)
end


-- world 里使用这个接口
function setBufferList(human, tb)
  local bufList = human.db.buf
  local bufLen = human.db.bufLen
  local len = 0
  for i=0,bufLen-1 do
    local buf =bufList[i]
    local config = BufferExcel[buf.id]
    if config then
      len = len + 1
      local buff = tb[len]  
      setBuffInfo(human,buff, buf, config)  
      if len>=16 then
          break
      end
    end
  end 
  tb[0] = len
end